package wechat.handler.work.template;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;

import wechat.handler.bean.request.template.TemplateInfoBean;
import wechat.handler.bean.request.template.TemplateValueBean;
import wechat.handler.bean.request.template.instance.BindNoticeBean;
import wechat.handler.bean.request.template.instance.PayNoticeBean;
import wechat.handler.bean.request.template.instance.VerifyNoticeBean;
import wechat.handler.request.template.ITemplate;
import wechat.handler.request.template.impl.Template;
import wechat.handler.resources.AppFilter;
import wechat.handler.resources.InstanceResource;
import wechat.handler.util.type.TemplateType;

public class SendTempleHandler {
	private Logger log = LoggerFactory.getLogger(SendTempleHandler.class);
	
	private ITemplate iTemplate;
	private InstanceResource instanceResource;
	
	public  SendTempleHandler() {
		iTemplate = new Template();
	}
	
	public void sendTemple(JSONObject jsonObject) throws Exception{
		String template_id = jsonObject.getString("template_id");
		String touser = jsonObject.getString("touser");
		
		if (template_id == null || "".equals(template_id)) {
			log.error("SendTempleHandler---sendTemple  jsonObject=" + jsonObject);
			throw new Exception("template_id is null or empty");
		}
		
		if (touser == null || "".equals(touser)) {
			log.error("SendTempleHandler---sendTemple  jsonObject=" + jsonObject);
			throw new Exception("touser is null or empty");
		}
		
		
		if (template_id.equalsIgnoreCase(TemplateType.Pay.getType())) {
			sendTempleForPay(jsonObject);
		}
		else if (template_id.equalsIgnoreCase(TemplateType.Verify.getType())) {
			sendTempleForVerify(jsonObject);
		}
		else if (template_id.equalsIgnoreCase(TemplateType.Bind.getType())) {
			sendTempleForBind(jsonObject);
		}
	}
	

	public void sendTemple(String appid,JSONObject jsonObject) throws Exception{
		String template_id = jsonObject.getString("template_id");
		String touser = jsonObject.getString("touser");
		
		instanceResource = AppFilter.chooseApp(appid);
		
		if (template_id == null || "".equals(template_id)) {
			log.error("SendTempleHandler---sendTemple  jsonObject=" + jsonObject);
			throw new Exception("template_id is null or empty");
		}
		
		if (touser == null || "".equals(touser)) {
			log.error("SendTempleHandler---sendTemple  jsonObject=" + jsonObject);
			throw new Exception("touser is null or empty");
		}
		
		
		if (template_id.equalsIgnoreCase(TemplateType.Pay.getType())) {
			sendTempleForPay(jsonObject);
		}
		else if (template_id.equalsIgnoreCase(TemplateType.Verify.getType())) {
			sendTempleForVerify(jsonObject);
		}
		else if (template_id.equalsIgnoreCase(TemplateType.Bind.getType())) {
			sendTempleForBind(jsonObject);
		}
	}
	
	private void sendTempleForPay(JSONObject jsonObject) throws Exception {
		String touser = jsonObject.getString("touser");
		String url = jsonObject.getString("url");
		String first = jsonObject.getString("first");
		String keyword1 = jsonObject.getString("keyword1");
		String keyword2 = jsonObject.getString("keyword2");
		String keyword3 = jsonObject.getString("keyword3");
		String remark = jsonObject.getString("remark");
		
		PayNoticeBean payNoticeBean = new PayNoticeBean();
		
		TemplateInfoBean parent = null;
		if (instanceResource != null) {
			parent = new TemplateInfoBean(instanceResource, TemplateType.Pay);
		}
		else {
			parent = new TemplateInfoBean(TemplateType.Pay);
		}

		parent.setTouser(touser);
		if (url != null && !url.equals("")) {
			parent.setUrl(url);
		}
		
		
		TemplateValueBean childrenBean = new TemplateValueBean();
		childrenBean.setValue(first);
		payNoticeBean.setFirst(childrenBean);
		
		childrenBean = new TemplateValueBean();
		childrenBean.setValue(keyword1);
		payNoticeBean.setKeyword1(childrenBean);
		
		childrenBean = new TemplateValueBean();
		childrenBean.setValue(keyword2);
		payNoticeBean.setKeyword2(childrenBean);
		
		childrenBean = new TemplateValueBean();
		childrenBean.setValue(keyword3);
		payNoticeBean.setKeyword3(childrenBean);
		
		childrenBean = new TemplateValueBean();
		childrenBean.setValue(remark);
		payNoticeBean.setRemark(childrenBean);
		
		parent.setData(payNoticeBean);
		
		iTemplate.sendTemplate(parent);
	}
	
	
	private void sendTempleForVerify(JSONObject jsonObject) throws Exception {
		String touser = jsonObject.getString("touser");
		String url = jsonObject.getString("url");
		String first = jsonObject.getString("first");
		String keyword1 = jsonObject.getString("keyword1");
		String keyword2 = jsonObject.getString("keyword2");
		String keyword3 = jsonObject.getString("keyword3");
		String remark = jsonObject.getString("remark");
		
		
		VerifyNoticeBean verifyNoticeBean = new VerifyNoticeBean();
		
		TemplateInfoBean parent = null;
		if (instanceResource != null) {
			parent = new TemplateInfoBean(instanceResource, TemplateType.Verify);
		}
		else {
			parent = new TemplateInfoBean(TemplateType.Pay);
		}

		parent.setTouser(touser);
		if (url != null && !url.equals("")) {
			parent.setUrl(url);
		}
		
		
		TemplateValueBean childrenBean = new TemplateValueBean();
		childrenBean.setValue(first);
		verifyNoticeBean.setFirst(childrenBean);
		
		childrenBean = new TemplateValueBean();
		childrenBean.setValue(keyword1);
		verifyNoticeBean.setKeyword1(childrenBean);
		
		childrenBean = new TemplateValueBean();
		childrenBean.setValue(keyword2);
		verifyNoticeBean.setKeyword2(childrenBean);
		
		childrenBean = new TemplateValueBean();
		childrenBean.setValue(keyword3);
		verifyNoticeBean.setKeyword3(childrenBean);
		
		childrenBean = new TemplateValueBean();
		childrenBean.setValue(remark);
		verifyNoticeBean.setRemark(childrenBean);
		
		parent.setData(verifyNoticeBean);
		
		iTemplate.sendTemplate(parent);
	}
	
	
	private void sendTempleForBind(JSONObject jsonObject) throws Exception {
		String touser = jsonObject.getString("touser");
		String url = jsonObject.getString("url");
		String first = jsonObject.getString("first");
		String keyword1 = jsonObject.getString("keyword1");
		String keyword2 = jsonObject.getString("keyword2");
		String remark = jsonObject.getString("remark");
		
		BindNoticeBean payTemplate = new BindNoticeBean();
		
		TemplateInfoBean parent = null;
		if (instanceResource != null) {
			parent = new TemplateInfoBean(instanceResource, TemplateType.Bind);
		}
		else {
			parent = new TemplateInfoBean(TemplateType.Pay);
		}

		parent.setTouser(touser);
		if (url != null && !url.equals("")) {
			parent.setUrl(url);
		}
		
		TemplateValueBean childrenBean = new TemplateValueBean();
		childrenBean.setValue(first);
		payTemplate.setFirst(childrenBean);
		
		childrenBean = new TemplateValueBean();
		childrenBean.setValue(keyword1);
		payTemplate.setKeyword1(childrenBean);
		
		childrenBean = new TemplateValueBean();
		childrenBean.setValue(keyword2);
		payTemplate.setKeyword2(childrenBean);
		
		childrenBean = new TemplateValueBean();
		childrenBean.setValue(remark);
		payTemplate.setRemark(childrenBean);
		
		parent.setData(payTemplate);
		
		iTemplate.sendTemplate(parent);
	}

}
